﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.WinControls.UI;
using LIM.model;
using LIM.controller;
using Telerik.WinControls.Data;
using p_Library;

namespace LIM.forms.uc_CRM_PhieuNhanMau_ChiTiet
{
    public partial class uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau : UserControl
    {
        radForm_CRM_PhieuNhanMau_ChiTiet parentForm = null;

        DanhMucPhongBanController DanhMucPhongBanController = new DanhMucPhongBanController();
        KiemNghiem_MauDuocTachController KiemNghiem_MauDuocTachController = new KiemNghiem_MauDuocTachController();

        public uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau()
        {
            InitializeComponent();
        }

        private void uc_CRM_PhieuNhanMau_ChiTiet_LuuTachMau_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_CRM_PhieuNhanMau_ChiTiet)this.ParentForm;

            DataLoad();
        }
        private void DataLoad()
        {           

            if (parentForm.tbl_CRM_PhieuNhanMau != null)
            {
                if (parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan != null)
                {
                    this.radGridView_DanhSachMauDuocNhan.DataSource = parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan;
                }                
            }
        }

        private void radGridView_DanhSachMauDuocNhan_SelectionChanged(object sender, EventArgs e)
        {
            BindingMauTach(false);
        }

        List<tbl_KiemNghiem_MauDuocTach> tbl_KiemNghiem_MauDuocTachs=new List<tbl_KiemNghiem_MauDuocTach>();
        private void BindingMauTach(bool isAll)
        {
            List<int> selectedRowIDMauDuocNhan = new List<int>();
            if (!isAll)
            {
                foreach (var item in this.radGridView_DanhSachMauDuocNhan.SelectedRows)
                {
                    tbl_CRM_PhieuNhanMau_MauDuocNhan tbl_CRM_PhieuNhanMau_MauDuocNhan = item.DataBoundItem as tbl_CRM_PhieuNhanMau_MauDuocNhan;
                    if (tbl_CRM_PhieuNhanMau_MauDuocNhan.RowID > 0)
                    {
                        tbl_CRM_PhieuNhanMau_MauDuocNhan.SoThuTu = selectedRowIDMauDuocNhan.Count + 1;
                        selectedRowIDMauDuocNhan.Add(tbl_CRM_PhieuNhanMau_MauDuocNhan.RowID);
                    }
                }
                this.radGridView_DanhSachMauDuocTach.DataSource = tbl_KiemNghiem_MauDuocTachs.Where(t => selectedRowIDMauDuocNhan.Contains(t.RowIDMauDuocNhan.GetValueOrDefault(0)));
            }
            else
            {
                for (int i = 0; i < tbl_KiemNghiem_MauDuocTachs.Count; i++)
                {
                    tbl_KiemNghiem_MauDuocTachs[i].SoThuTu = i + 1;
                }
                this.radGridView_DanhSachMauDuocTach.DataSource = tbl_KiemNghiem_MauDuocTachs;
            }
            
        }
                
        private void radMenuItem_ThucHienTachMau_Click(object sender, EventArgs e)
        {
            tbl_KiemNghiem_MauDuocTachs = new List<tbl_KiemNghiem_MauDuocTach>();

            foreach (var item in parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan)
            {
                // Cần bổ sung code phải loại trừ trường hợp đã tách rồi và tách lại lần nữa. 

                List<tbl_DanhMuc_PhongBan> tbl_DanhMuc_PhongBans = new List<tbl_DanhMuc_PhongBan>();
                foreach (var item2 in item.tbl_CRM_PhieuNhanMau_ChiTieuKiemNghiem)
                {
                    tbl_DanhMuc_PhongBan tbl_DanhMuc_PhongBan= DanhMucPhongBanController.LayPhongBanByMaPhongBan(item2.tbl_DanhMuc_ChiTieu.MaPhongBan);
                    if (!tbl_DanhMuc_PhongBans.Contains(tbl_DanhMuc_PhongBan))
                    {
                        tbl_DanhMuc_PhongBans.Add(tbl_DanhMuc_PhongBan);

                        tbl_KiemNghiem_MauDuocTach tbl_KiemNghiem_MauDuocTach = new tbl_KiemNghiem_MauDuocTach();
                        tbl_KiemNghiem_MauDuocTach.MaSoMauDuocTach = item.MaSoMau + "-" + (tbl_DanhMuc_PhongBans.Count + 1);
                        //tbl_KiemNghiem_MauDuocTach.SoThuTu = (tbl_DanhMuc_PhongBans.Count + 1);
                        tbl_KiemNghiem_MauDuocTach.RowIDMauDuocNhan = item.RowID;

                        tbl_KiemNghiem_MauDuocTachs.Add(tbl_KiemNghiem_MauDuocTach);
                    }
                }
            }

            BindingMauTach(true);
        }

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            //foreach (var item in parentForm.tbl_CRM_PhieuNhanMau.tbl_CRM_PhieuNhanMau_MauDuocNhan)
            //{
            //    if (item.RowID > 0)
            //    {
            //        foreach (var item2 in tbl_KiemNghiem_MauDuocTachs.Where(w => w.RowIDMauDuocNhan == item.RowID))
            //        {
                        
            //        }
            //    }
            //}

            bool checkSuccess = true;
            foreach (var item in tbl_KiemNghiem_MauDuocTachs)
            {
                int outCode = 0;
                string outMess = "";
                if (checkSuccess)
                    checkSuccess = KiemNghiem_MauDuocTachController.SaveObject(item, out outMess, out outCode);
                else
                    KiemNghiem_MauDuocTachController.SaveObject(item, out outMess, out outCode);
            }
            if (checkSuccess)
                RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Lưu tách mẫu", "Đã lưu thành công tách mẫu!");
            else
                RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Lưu tách mẫu", "Một số mẫu được tách không được lưu!");

            
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {

        }

    }
}
